package logic

import (
	"context"
	"fmt"
	"projectes/models/mysql"
	"projectes/models/redis"

	"projectes/projectrpc/internal/svc"
	"projectes/projectrpc/projectrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *projectrpc.LoginRequest) (*projectrpc.LoginResponse, error) {
	// todo: add your logic here and delete this line

	var Login mysql.User
	mysql.DB.Debug().Model(&Login).Where("mobile = ?", in.Mobile).Find(&Login)

	if Login.ID == 0 {
		return nil, fmt.Errorf("账户未注册")
	}
	if Login.Password != in.Password {
		return nil, fmt.Errorf("密码不正确,请重新输入")
	}
	get := redis.Client.Get("sendSms" + in.Mobile + "login")

	if get.Val() != in.SendSms {
		return nil, fmt.Errorf("短信验证失败")
	}

	return &projectrpc.LoginResponse{Id: int64(Login.ID)}, nil
}
